package com.sun.showcase.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import com.sun.showcase.biz.basic.service.impl.FtpClientServiceImpl;
import com.sun.showcase.biz.basic.utils.FtpClientConstants;



//文件操作工具类
public class FileUpload {
	//上传文件
	public static String upload(String folderName,MultipartFile file) throws IOException{
		//图片路径
		String  ffile ="/"+folderName+ "/"+DateUtil.getDays();
		//图片原名称
		String fileOrigName=file.getOriginalFilename();
		//图片名称
		String fileNewName=UuidUtil.get32UUID()+fileOrigName.substring(file.getOriginalFilename().lastIndexOf("."));
		
		InputStream ins=file.getInputStream();
		
		//获取实例
		FtpClientServiceImpl ftpClientServiceImpl =(FtpClientServiceImpl) SpringApplicationContextHolder.getBean("ftpClientServiceImpl");
		FtpClientConstants ftpClientConstants=(FtpClientConstants)SpringApplicationContextHolder.getBean("ftpClientConstants");
		//上传
		ftpClientServiceImpl.saveFileAsStream(ins, ffile, fileNewName);
		
		//ftp图片请求路径
		return ftpClientConstants.getPrefixUrl()+ffile+"/"+fileNewName;
	}
	
	//将网络图片上传至服务器
	public static String uploadNet(String folderName,String fileUrl) throws IOException{
		//图片路径
		String  ffile ="/"+folderName+ "/"+DateUtil.getDays();
		//图片名称
		String fileNewName=UuidUtil.get32UUID()+".jpg";
		// 构造URL
		URL url = new URL(fileUrl);
		// 打开连接
		URLConnection con = url.openConnection();
		//设置请求超时为5s
		con.setConnectTimeout(5*1000);
		// 输入流
		InputStream ins = con.getInputStream();
		
		//获取实例
		FtpClientServiceImpl ftpClientServiceImpl =(FtpClientServiceImpl) SpringApplicationContextHolder.getBean("ftpClientServiceImpl");
		FtpClientConstants ftpClientConstants=(FtpClientConstants)SpringApplicationContextHolder.getBean("ftpClientConstants");
		//上传
		ftpClientServiceImpl.saveFileAsStream(ins, ffile, fileNewName);
		
		//关闭流
		ins.close();
		//ftp图片请求路径
		return ftpClientConstants.getPrefixUrl()+ffile+"/"+fileNewName;
	}
	
	//删除图片 给出图片的完整url
	public static boolean delete(String fileUrl){
		//去掉ftpserver地址
		FtpClientServiceImpl ftpClientServiceImpl =(FtpClientServiceImpl) SpringApplicationContextHolder.getBean("ftpClientServiceImpl");
		FtpClientConstants ftpClientConstants=(FtpClientConstants)SpringApplicationContextHolder.getBean("ftpClientConstants");
		String path=fileUrl.replace(ftpClientConstants.getPrefixUrl(), "");
		//获取文件名
		String[] fileNames=path.split("/");
		String fileName=fileNames[fileNames.length-1];
		//获取文件夹名
		String folder=path.replace("/"+fileName, "");
		return ftpClientServiceImpl.deleteFile(folder, fileName);
	}
	
	//上传本地文件
	public static String uploadLocal(String folderName,MultipartFile file) throws IOException{
		//图片路径
		String  ffile = folderName+ "/"+DateUtil.getDays();
		//图片原名称
		String fileOrigName=file.getOriginalFilename();
		
		int extIndex = file.getOriginalFilename().lastIndexOf(".");
		
		//图片名称
		//String fileNewName=exFileName +"-"+ UuidUtil.get32UUID()+fileOrigName.substring(extIndex);
		String fileNewName=UuidUtil.get32UUID()+fileOrigName.substring(extIndex);
		
		FtpClientConstants ftpClientConstants=(FtpClientConstants)SpringApplicationContextHolder.getBean("ftpClientConstants");
		
		FileUtils.writeByteArrayToFile(new File(ftpClientConstants.getDisk()+File.separator+ffile+File.separator+fileNewName),file.getBytes());
		

		return ffile+"/"+fileNewName;
	}
	//上传本地文件
	public static String uploadLocal2(String folderName,File file) throws IOException{
		//图片路径
		String  ffile = folderName+ "/"+DateUtil.getDays();
		//图片原名称
		String fileOrigName=file.getName();
		
		int extIndex = file.getName().lastIndexOf(".");
		
		//图片名称
		//String fileNewName=exFileName +"-"+ UuidUtil.get32UUID()+fileOrigName.substring(extIndex);
		String fileNewName=UuidUtil.get32UUID()+fileOrigName.substring(extIndex);
		
		FtpClientConstants ftpClientConstants=(FtpClientConstants)SpringApplicationContextHolder.getBean("ftpClientConstants");
		
		FileUtils.copyFile(file, new File(ftpClientConstants.getDisk()+File.separator+ffile+File.separator+fileNewName));
		return ffile+"/"+fileNewName;
	}
	//上传本地文件
	public static String uploadLocal3(String folderName,String url) throws IOException{
		//图片路径
		String  ffile = folderName+ "/"+DateUtil.getDays();
		//图片名称
		//String fileNewName=exFileName +"-"+ UuidUtil.get32UUID()+fileOrigName.substring(extIndex);
		String fileNewName=UuidUtil.get32UUID()+".jpg";
		
		FtpClientConstants ftpClientConstants=(FtpClientConstants)SpringApplicationContextHolder.getBean("ftpClientConstants");
		
		FileUtils.copyURLToFile(new URL(url), new File(ftpClientConstants.getDisk()+File.separator+ffile+File.separator+fileNewName));
		return ffile+"/"+fileNewName;
	}
}
